import {defineStore} from 'pinia'
import {CheckLogin} from "../../wailsjs/go/service/BackService";
import {reactive} from "vue";
import {comdto, dbdto} from "../../wailsjs/go/models";

export interface MessageInfo{
    avatar: string
    role: string
    content: string | ""
    ClipboardTexts: Array<string> | []
    useTime?:number  | 0,
    createdAt?:string
    contentId?:number,
    isHanding:boolean,
    isError:boolean,
    errorMsg:string | "",
}
export const useGlobalStore=defineStore({
    id:"global",
    state:()=>({
        IsLogin:false,
        BackUrl:"",
        Notices:reactive<Record<string,any>>({}),
        UserInfo:reactive<comdto.UserDtoInfo>(new comdto.UserDtoInfo()),
        //记录快捷结果面板的消息
        CurrMessages:[] as Array<MessageInfo>,
        CurrChatDialogId:0,
        CurrChatToolId:0,
        CurrChatToolInfo:{} as dbdto.UserToolInfo,
        //记录快捷键信息
        CurrMouseButtonLongClick:{
            code:"",
            lang:"",
        },
        CurrKeyBoardCombineKey:{
            code:"",
            lang:"",
        },
    }),
    actions:{
        SetToolInfo(toolInfo:dbdto.UserToolInfo){
          this.CurrChatToolInfo=  toolInfo
        },
        ClearMessages(){
            this.CurrMessages=[]
        },
        MessageAddContent(NotifyInfo:any){
            if(this.CurrChatDialogId != NotifyInfo["info"]["chat_dialog_id"]){
                //新的对话 则清空历史
                this.CurrMessages=[]
            }
           this.CurrChatDialogId=NotifyInfo["info"]["chat_dialog_id"]
           this.CurrChatToolId=NotifyInfo["info"]["tool_id"]
           this.CurrChatToolInfo=NotifyInfo["tool"]
            let avatar="/static/images/gpt.png"
           let item:MessageInfo= {
               avatar: avatar,
               content: NotifyInfo["info"]["content"],
               role: NotifyInfo["info"]["sender_role"],
               ClipboardTexts: NotifyInfo["clipboard_texts"] ,

               useTime:NotifyInfo["info"]["use_time"],
               createdAt:NotifyInfo["info"]["created_at"],
               contentId:NotifyInfo["info"]["id"],
               isHanding:NotifyInfo["info"]["is_handing"],
               errorMsg:NotifyInfo["info"]["error_msg"],
               isError:NotifyInfo["info"]["is_error"],
           }
            const foundIndex = this.CurrMessages.findIndex(message => message.contentId === item.contentId);
            if (foundIndex !== -1) {
                // item 存在于 this.CurrMessages 数组中，进行修改操作
                this.CurrMessages[foundIndex] = item;
            } else {
                // item 不存在于 this.CurrMessages 数组中，将其追加到数组末尾
                this.CurrMessages.push(item);
            }

            console.log("MessageAddContent",item)

        },
        SetMouseLongData(code :string,lang:string){
            this.CurrMouseButtonLongClick.lang=lang
            this.CurrMouseButtonLongClick.code=code
        },

        SetMHotKeyData(code :string,lang:string){
            this.CurrKeyBoardCombineKey.lang=lang
            this.CurrKeyBoardCombineKey.code=code
        },
        LoginResSet(UserInfo:comdto.UserDtoInfo){
            if (UserInfo) {
                this.IsLogin = true
                this.UserInfo = UserInfo
            } else {
                this.IsLogin = false
            }
        },
        async CheckLogin(): Promise<Boolean> {

            //console.log(res,"login res")
            let res = await CheckLogin()
            this.LoginResSet(res)
            return this.IsLogin
        }
    }
})